package edu.zjucst.spb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.zjucst.spb.dao.mapper.ActivityMapper;
import edu.zjucst.spb.dao.spbuser.SpbUserDao;
import edu.zjucst.spb.domain.entity.Activity;
import edu.zjucst.spb.domain.entity.SpbUser;
import edu.zjucst.spb.service.SpbUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SpbUserServiceImpl implements SpbUserService {

    @Autowired
    private SpbUserDao spbUserDao;

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    @Transactional
    public int addUser(SpbUser spbUser) {
        return spbUserDao.insert(spbUser);
    }

    @Override
    @Transactional
    public int deleteUser(Long userId) {
        return spbUserDao.deleteById(userId);
    }

    @Override
    @Transactional
    public int deleteUsers(List<SpbUser> spbUserList) {
        int cnt = 0;
        for (SpbUser spbUser : spbUserList) {
            cnt += spbUserDao.deleteById(spbUser.getId());
        }
        return cnt;
    }

    @Override
    @Transactional
    public int updateUser(SpbUser spbUser) {
        return spbUserDao.updateById(spbUser);
    }

    @Override
    @Transactional
    public int updateUsers(List<SpbUser> spbUserList) {
        int cnt = 0;
        for (SpbUser spbUser : spbUserList) {
            cnt += spbUserDao.updateById(spbUser);
        }
        return cnt;
    }

    @Override
    @Transactional
    public List<SpbUser> findUser(SpbUser spbUser) {
//        Long id = spbUser1.getId();
//        String username = spbUser1.getUsername();
//        String roleName = spbUser1.getRoleName();
//        Date registerTime = spbUser1.getRegisterTime();
//
//        SelectStatementProvider selectStatement = SqlBuilder.select(spbUserMapper.selectList)
//            .from(spbUser)
//            .where(SpbUserDynamicSqlSupport.id, isEqualToWhenPresent(id))
//            .and(SpbUserDynamicSqlSupport.username, isLikeWhenPresent(username))
//            .and(SpbUserDynamicSqlSupport.roleName, isEqualToWhenPresent(roleName))
//            .and(SpbUserDynamicSqlSupport.registerTime, isGreaterThanOrEqualToWhenPresent(registerTime))
//            .build()
//            .render(RenderingStrategies.MYBATIS3);
//
//        return spbUserMapper.selectMany(selectStatement);

//        return spbUserDao.selectList(new LambdaQueryWrapper<SpbUser>()
//            .eq(SpbUser::getId, spbUser.getId())
//            .eq(SpbUser::getUsername, spbUser.getUsername())
//            .eq(SpbUser::getRoleName, spbUser.getRoleName())
//            .gt(SpbUser::getRegisterTime, spbUser.getRegisterTime())
//        );
        return spbUserDao.selectList(new LambdaQueryWrapper<SpbUser>()
            .eq(!ObjectUtils.isEmpty(spbUser.getId()), SpbUser::getId, spbUser.getId())
            .eq(!ObjectUtils.isEmpty(spbUser.getUsername()), SpbUser::getUsername, spbUser.getUsername())
            .eq(!ObjectUtils.isEmpty(spbUser.getRoleName()), SpbUser::getRoleName, spbUser.getRoleName())
            .gt(!ObjectUtils.isEmpty(spbUser.getRegisterTime()), SpbUser::getRegisterTime, spbUser.getRegisterTime())
        );
    }

    @Override
    public SpbUser findUserByNumber(String userNumber) {
        QueryWrapper<SpbUser> wrapper = new QueryWrapper<SpbUser>();
        if (userNumber != null) {
            wrapper.eq("user_number", userNumber);
        }
        return spbUserDao.selectOne(wrapper);
    }

    @Override
    public IPage<SpbUser> selectByPhase(Page<SpbUser> page, String partyBranch, String developmentPhase) {
        QueryWrapper<SpbUser> wrapper = new QueryWrapper<SpbUser>();
        if (partyBranch != null) {
            wrapper.eq("party_branch", partyBranch);
        }
        if (developmentPhase != null) {
            wrapper.eq("development_phase", developmentPhase);
        }
        return spbUserDao.selectPage(page, wrapper);
    }

    @Override
    public IPage<SpbUser> selectByState(Page<Activity> page, String partyBranch, String activityNumber, String state) {
        QueryWrapper<Activity> wrapper = new QueryWrapper<Activity>();
        if (partyBranch != null) {
            wrapper.eq("party_branch", partyBranch);
        }
        if (activityNumber != null) {
            wrapper.eq("activity_number", activityNumber);
        }
        if (state != null) {
            wrapper.eq("state", state);
        }
        Page<Activity> activityPage = activityMapper.selectPage(page, wrapper);
        List<SpbUser>  spbUsers     = new ArrayList<>();
        for (Activity activity : activityPage.getRecords()
        ) {
            SpbUser userByNumber = findUserByNumber(activity.getUserNumber());
            spbUsers.add(userByNumber);
        }
        Page<SpbUser> spbUserPage = new Page<>();
        spbUserPage.setRecords(spbUsers);
        spbUserPage.setPages(activityPage.getPages());
        spbUserPage.setCurrent(activityPage.getCurrent());
        spbUserPage.setSize(activityPage.getSize());
        return spbUserPage;
    }

    @Override
    public IPage<SpbUser> selectByRoleName(Page<SpbUser> page, String partyBranch, String roleName) {
        QueryWrapper<SpbUser> wrapper = new QueryWrapper<SpbUser>();
        if (partyBranch != null) {
            wrapper.eq("party_branch", partyBranch);
        }
        if (roleName != null) {
            wrapper.eq("role_name", roleName);
        }
        return spbUserDao.selectPage(page, wrapper);
    }

}
